#include<QJsonObject>
#include<QJsonArray>
#include<QJsonParseError>
#include<QJsonDocument>

#include "network.h"

NetWork::NetWork(QString url)
{
    m_url = url;
    qDebug()<<m_url;
    QUrl qurl(m_url);
    QNetworkRequest req(qurl);
    m_reply = m_nam.get(req);
    connect(m_reply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(onEror(QNetworkReply::NetworkError)));
    connect(m_reply,SIGNAL(finished()),this,SLOT(onFinished()));

}
NetWork::~NetWork()
{

}
void NetWork::onEror(QNetworkReply::NetworkError code)
{
    qDebug()<<code;
}

void NetWork::onFinished()
{
    int status = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    qDebug()<<status;
    if(m_reply->error() == QNetworkReply::NoError){
        m_data = m_reply->readAll();
//        QJsonParseError jsonpe;
//        QJsonDocument json = QJsonDocument::fromJson(data, &jsonpe);
//        if(jsonpe.error == QJsonParseError::NoError)
//        {
//           if(json.isArray()){
//              QJsonArray obj = json.array();
//              qDebug()<<obj.size();
//              for(int i=0;i<obj.size();i++){
//                 qDebug()<<obj[i].toString();
//              }
//          }
//        };
        //qDebug()<<m_data;
    }
}

QByteArray NetWork::getData(){
    //qDebug()<<m_data;
    return m_data;
}

QString NetWork::getUrl(){
    return m_url;
}
